package friends;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.Arrays;

public class SecondMapper extends Mapper<LongWritable, Text,Text,Text> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获取一行数据A	C,B,D,F,E,O,
        String line = value.toString();
        //切分数据
        String[] fields = line.split("\t");
        //获取每个人的好友数据
        String user_friend = fields[0];
        //获取号主列表
        String[] users = fields[1].split(",");
        //将用户按字典顺序排序
        Arrays.sort(users);
        //多重for循环的执行顺序是，最外面的for循环执行一次，里面的同层级的for循环就会执行一轮
       //将用户两两排列组合
        for (int i = 0; i <users.length-1 ; i++) {
            for (int j = i+1; j < users.length; j++) {
                String userAnduser=users[i]+"-"+ users[j];
                //k,v
                context.write(new Text(userAnduser),new Text(user_friend));

            }

        }


    }
}
